/**
 * Copyright (C) 2010 Daniel <daniel.petisme@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * 
 */
package fr.daniel.todolist.model.pojo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 * @author daniel
 * 
 */
@SuppressWarnings("serial")
/**
 * Represent a level of emergency
 */
@Entity
@Table(name = "PRIORITY")
public class Priority implements Serializable {

	private static Logger log = Logger.getLogger(Priority.class);

	/** The highest level of priority possible */
	public static final Priority HIGHEST = new Priority(Integer.MAX_VALUE,
			"High");

	/** The lowest level of priority possible */
	public static final Priority LOWEST = new Priority(0, "Low");

	@Id
	@Column(name = "PRIORITY_ID", nullable = false)
	@GeneratedValue(strategy=GenerationType.SEQUENCE)
	private int id;

	/** The level of the priority */
	@Column(name = "PRIORITY_LEVEL", nullable = false)
	private int level = 0;

	/** The name of the priority */
	@Column(name = "PRIORITY_LABEL", length = 50, nullable = false)
	private String label;

	@OneToMany(cascade = CascadeType.ALL, targetEntity = Todo.class, mappedBy="priority")
	private List<Todo> todos = new ArrayList<Todo>();

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @return the level
	 */
	public int getLevel() {
		return level;
	}

	/**
	 * @param level
	 *            the level to set
	 */
	public void setLevel(int level) {
		this.level = level;
	}

	/**
	 * @return the label
	 */
	public String getLabel() {
		return label;
	}

	/**
	 * @param label
	 *            the label to set
	 */
	public void setLabel(String label) {
		this.label = StringUtils.trim(label);
	}

	/**
	 * @return the todos
	 */
	public List<Todo> getTodos() {
		return todos;
	}

	/**
	 * @param todos
	 *            the todos to set
	 */
	public void setTodos(List<Todo> todos) {
		this.todos = todos;
	}

	/**
	 * Default Constructor
	 */
	public Priority() {
		setLevel(0);
		setLabel("default label");
		log.debug(new StringBuilder("New : ").append(this).toString());
	}

	/**
	 * @param level
	 *            The level of the priority
	 * @param label
	 *            The name of this priority
	 */
	public Priority(int level, String label) {

		setLevel(level);
		setLabel(label);
		log.debug(new StringBuilder("New : ").append(this).toString());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((label == null) ? 0 : label.hashCode());
		result = prime * result + level;
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {

		boolean response = false;

		if (this == obj) {
			response = true;
		} else {
			if (obj instanceof Priority) {
				Priority other = (Priority) obj;
				if (other.getLabel() != null) {
					if (label.equals(other.getLabel())
							&& level == other.getLevel()) {
						if (hashCode() == other.hashCode()) {
							response = true;
						}
					}
				}
			}
		}
		return response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("Priority [");
		if (label != null) {
			builder.append("label=");
			builder.append(label);
			builder.append(", ");
		}
		builder.append("level=");
		builder.append(level);
		builder.append("]");
		return builder.toString();
	}
}
